﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace SubtitlesCleaner
{
    public class SubtitleFile
    {
        public string fileName;
        public double frameRate;
        List<string> subtitleLines = new List<string>();
        List<Subtitle> subtitles = new List<Subtitle>();

        public SubtitleFile(string path, double rate, List<string> lines)
        {
            fileName = path;
            frameRate = rate;
            subtitleLines = lines;
            decodeLines();
        }

        public bool checkSubRip()
        {
            if (subtitleLines.Count == 0)
                return false;

            return checkSubRip(subtitleLines[0]);
        }

        public bool checkSubRip(string line)
        {
            int i1 = line.IndexOf("{");
            int i2 = line.IndexOf("[");
            int i3 = line.IndexOf(":");

            if ((i1 < 0) && (i2 < 0) & (i3 < 0))
            {
                try
                {
                    int i = Int32.Parse(line);
                    if (i == 1)
                    {
                        return true;
                    }
                }
                catch (FormatException)
                {
                    SubtitleFormatException.Write(fileName, line);
                }
            }
            return false;
        }

        private void decodeLines()
        {
            bool isSubRip = false;
            int counter = 1;
            Subtitle lastSubtitle = null;

            foreach (string line in subtitleLines)
            {
                if (string.IsNullOrEmpty(line))
                {
                    if (isSubRip)
                        if (subtitles.Count < 1)
                            counter++;
                        else
                        {
                            if (subtitles.Count >= counter)
                                if (subtitles[counter - 1].lines.Count > 0)
                                    counter++;
                        }
                    continue;
                }

                if (!isSubRip)
                {
                    isSubRip = checkSubRip(line);
                    if (isSubRip)
                    {
                        subtitles.Add(new Subtitle());
                        continue;
                    }
                }

                if (isSubRip)
                {
                    if (subtitles.Count == counter)
                    {
                        Subtitle st = subtitles[counter - 1];
                        if (!st.isTimeSet()) //time
                            st.decodeSubRipTime(line);
                        else
                            st.lines.Add(line);
                    }
                    else
                    {
                        subtitles.Add(new Subtitle());
                    }
                }
                else
                {
                    try
                    {
                        subtitles.Add(new Subtitle(line, fileName, frameRate, lastSubtitle));
                    }
                    catch (FormatException)
                    {
                        SubtitleFormatException.Write(fileName, line);
                    }
                }
                lastSubtitle = subtitles[subtitles.Count - 1];
            }
        }

        public override string ToString()
        {
            return fileName + " @ " + frameRate + " fps";
        }

        public String exportToSubRip()
        {
            Encoding encoding = Encoding.GetEncoding("windows-1250");
            String extension = DateTime.Now.Ticks.ToString();

            string targetFileName = fileName + "." + extension;

            StreamWriter sw = new StreamWriter(targetFileName, false, encoding);
            Subtitle.ResetCounter();
            foreach (Subtitle line in subtitles)
            {
                sw.WriteLine(line);
            }
            sw.Close();
            return targetFileName;
        }
    }
}
